package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.util.Log
import android.view.View
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.activity.OrganizationActivity
import cc.vv.btpartyjob.jnimodule.activity.QuanXainOrganizationActivity
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.ManagerViewPageAdapter
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.UnitReportActivityDelegate
import cc.vv.btpartyjob.partymanager.fragment.UnitRegisterNoFragment
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.dawning.utiming.task.fragment.UnitRegisterYesFragment

/**
 * 单位报到
 * Created by Chen.qi on 2018/10/27.
 */
class UnitReportActivity : BaseActivityMVP<UnitReportActivityDelegate>() {

    /**
     * ViewPager适配器
     */
    private var mAdapter: ManagerViewPageAdapter? = null

    /**
     * Fragment集合
     */
    private var mFragments: ArrayList<Fragment>? = null

    override fun initData() {
        viewDelegate?.setTopBarTitle("单位报到")
        mFragments = ArrayList()
        val registerYesFragment = UnitRegisterYesFragment()
        val registerNoFragment = UnitRegisterNoFragment()
        mFragments!!.add(registerYesFragment)
        mFragments!!.add(registerNoFragment)
        mAdapter = ManagerViewPageAdapter(supportFragmentManager, mFragments!!)
        viewDelegate?.setAdapterData(mAdapter!!)
        viewDelegate?.setRegisterYesSelect(true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                val intent = Intent(this@UnitReportActivity, QuanXainOrganizationActivity::class.java)
                intent.putExtra(IntentJniKey.INTENT_KEY_ORG_TYPE, 2)
                startActivityForResult(intent, 100)
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //已报到
            viewDelegate?.setRegisterYesSelect(true)
            viewDelegate?.setViewPagerPos(0)
        }, R.id.rl_aut_registerYes)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //未报到
            viewDelegate?.setRegisterYesSelect(false)
            viewDelegate?.setViewPagerPos(1)
        }, R.id.rl_aut_regNo)

        viewDelegate?.bindViewPagerLisener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    viewDelegate?.setRegisterYesSelect(true)
                } else {
                    viewDelegate?.setRegisterYesSelect(false)
                }
            }
        })
    }

    override fun getDelegateClass(): Class<UnitReportActivityDelegate> {
        return UnitReportActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            val orgId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            val orgType = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_TYPE)
            try {
                val yesFrag = mFragments!![0] as UnitRegisterYesFragment
                yesFrag.chooseYesFragment(orgId, orgType)
                val noFrag = mFragments!![1] as UnitRegisterNoFragment
                noFrag.chooseNoFragment(orgId, orgType)
            } catch (e: Exception) {
            }
        }
    }
}